<?php
# 文件名称:view.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_view
{
	var $_name = null;
	
	var $_models = array();
	
	var $_defaultModel = null;
	//var $_parent_name = '';
	
	var $_escape = 'htmlspecialchars';
	
	var $_charset = 'UTF-8';
	
	var $_template = null;
	
	var $imagePath = '';
	
	var $cssPath = '';
	
	function C_view()
	{
		$SYS =& get_instance();		
		$SYS->doc->setHeadData(array('title'=>C_text::_('adminTitle').' - Powered by MetInfo'));
		$SYS->doc->setMetaData('generator',$SYS->config->item('metaauthor'));		
		$SYS->doc->setMetaData('description',$SYS->config->item('metadesc'));
		$SYS->doc->setMetaData('keywords',$SYS->config->item('metakeys'));		
		$this->imagePath = WEBURL.'/templates/'.$SYS->config->item('theme').'/'.'images/';
		$this->cssPath = WEBURL.'/templates/'.$SYS->config->item('theme').'/'.'css/';
		$SYS->doc->addFavicon(SITEURL.'/favicon.ico');

		// 如果魔术方法 __get() or __set()存在则引用无法建立
		//$this->_assign_libraries( (method_exists($this, '__get') OR method_exists($this, '__set')) ? FALSE : TRUE );
		
		// 初始化时设置排除对自己的引用
		//$this->_parent_name = ucfirst(get_class($this));
	}
	
	
	/**
	 * 建立对所有已经初始化对象的引用
	 *
	 * @访问限制 private
	function _assign_libraries($use_reference = TRUE)
	{
		$SYS =& get_instance();
		foreach (array_keys(get_Object_vars($SYS)) as $key)
		{
			if ( ! isset($this->$key) AND $key != $this->_parent_name)
			{				
				if ($use_reference == TRUE)
				{
					$this->$key = NULL;
					$this->$key =& $SYS->$key;
				}
				else
				{
					$this->$key = $SYS->$key;
				}
			}
		}		
	}*/
	
	function display($tpl = null,$path = null)
	{
		$SYS =& get_instance();
		$CFG = &$SYS->config;		
		$tpl = $tpl==null?$this->getName():$tpl;
		if($path == null)
		{
			//后台
			$path = PATH_THEMES.DS.$CFG->item('theme');
		}
		//清空
		$this->_output = null;		
		if ( ! file_exists($path.DS.$tpl.".html"))
		{
			show_error('Unable to locate the template you have specified: '.$this->getName());
		}
		include_once PATH_THEMES.DS."common.php";
		$this->render($SYS->doc->getHeadData());
		//加载模板
		include $path.DS.$tpl.".html";
	}
	function getName()
	{
		$name = $this->_name;
		if (empty( $name ))
		{
			$r = null;
			if (!preg_match('/(.*(view)?.*)View$/i', get_class($this), $r)) {
				die("C_view::getName() : Cannot get or parse class name.");
			}
			if (strpos($r[1], "view"))
			{
				return false;
			}
			$name = strtolower( $r[1] );
		}
		return $name;
	}
	function assign()
	{
		$arg0 = @func_get_arg(0);
		$arg1 = @func_get_arg(1);
		if (is_object($arg0))
		{
			foreach (get_object_vars($arg0) as $key => $val)
			{
				if (substr($key, 0, 1) != '_') {
					$this->$key = $val;
				}
			}
			return true;
		}
		if (is_array($arg0))
		{
			foreach ($arg0 as $key => $val)
			{
				if (substr($key, 0, 1) != '_') {
					$this->$key = $val;
				}
			}
			return true;
		}

		if (is_string($arg0) && substr($arg0, 0, 1) != '_' && func_num_args() > 1)
		{
			$this->$arg0 = $arg1;
			return true;
		}
		return false;
	}
	/**
	* 引用变量赋值
	*
	* 例子
	* <code>
	* $view = new C_view();
	*
	* // assign by name and value
	* $view->assignRef('var1', $ref);
	*
	* // assign directly
	* $view->ref =& $var1;
	* </code>
	*/
	function assignRef($key, &$val)
	{
		if (is_string($key) && substr($key, 0, 1) != '_')
		{
			$this->$key =& $val;
			return true;
		}
		return false;
	}
	

	 // 从模型中获取数据
	 
	function &get( $property, $default = null )
	{
		// 默认model
		if (is_null($default)) {
			$model = $this->_defaultModel;
		} else {
			$model = strtolower( $default );
		}
		// 获取数据
		if (isset( $this->_models[$model] ))
		{
			// 调用model方法
			$method = 'get'.ucfirst($property);
			if (method_exists($this->_models[$model], $method))
			{
				// 执行
                $result = $this->_models[$model]->$method();
                return $result;
			}
		}
		
		//没有找到时
		return $property;
	}
	//过滤
	function escape($var)
    {
        if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
            return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_charset);
        }
        return call_user_func($this->_escape, $var);
    }
	function setEscape($spec)
    {
        $this->_escape = $spec;
    }
	function &getModel( $name = null )
	{
		if ($name === null) {
			$name = $this->_defaultModel;
		}
		return $this->_models[strtolower( $name )];
	}
	function &setModel( &$model, $default = false )
	{
		$name = strtolower($model->getName());
		$this->_models[$name] = &$model;
		//$this->_assign_libraries(TRUE);//建立对模型的引用
		if ($default) {
			$this->_defaultModel = $name;
		}
		return $model;
	}
	function render($tmp = null)
	{		
		$data='';
		//meta
		foreach($tmp['metaTags'] as $key => $val)
		{
			if($key=='http-equiv')
			{
				foreach($val as $metatype => $content)
			$data.='<meta http-equiv="'.$metatype.'" content="'.$content.'" />';
			}else
			{
				foreach($val as $metatype => $content)
					$data.='<meta name="'.$metatype.'" content="'.$content.'" />';
			}
		}
		
		$data.='<meta name="generator" content="'.$tmp['generator'].'" />';
		$data.='<meta name="keywords" content="'.$tmp['_keywords'].'" />';
		$data.='<meta name="description" content="'.$tmp['description'].'" />';
		//link
		foreach($tmp['links'] as $key => $val)
		{
			$data.=$val;
		}
		//css
		foreach($tmp['styleSheets'] as $key => $val)
		{
			$data.='<link rel="stylesheet" href="'.$key.'" type="'.$val['mime'].'" media="'.$val['media'].'" />';
		}
		$this->assignRef('headData',$data);
		unset($data);
		$data = '';		
		//js
		foreach($tmp['scripts'] as $key => $val)
		{
			$data.='<script type="'.$val.'" src="'.$key.'" ></script>';				
		}
		foreach($tmp['script'] as $key => $val)
		{
			$data.='<script type="'.$key.'">'.$val.'</script>';
		}
		$this->assignRef('js',$data);
		
		unset($tmp);
	}
}
?>